import axios from 'axios'

const service = axios.create({
    // 联调
    baseURL:  import.meta.env.VITE_RES_URL || "/proxy/",
    // 是否跨站点访问控制请求
    withCredentials: true,
    timeout: 30000,
    // transformRequest: [
    //     (data) => {
    //         data = JSON.stringify(data)
    //         return data
    //     },
    // ],
    validateStatus() {
        // 使用async-await，处理reject情况较为繁琐，所以全部返回resolve，在业务代码中处理异常
        return true
    },
    transformResponse: [
        (data) => {
            if (typeof data === 'string' && data.startsWith('{') || data.startsWith('[')) {
                data = JSON.parse(data)
            }
            return data
        },
    ],
})

// 请求拦截器
service.interceptors.request.use(
    (config) => {
        console.log("看看配置是什么",config,import.meta.env)
        return config
    },
    (error) => {
        return Promise.resolve(error)
    }
)

// 响应拦截器
service.interceptors.response.use(
    (response) => {
        return response

    },
    (error) => {
        return Promise.resolve(error)
    }
)

export function json(url,data=null){
    return service({
        method:"post",
        url:url,
        data:data,
        headers:{'Content-Type': 'application/json;charset=utf-8',}
    })
}
export function update(url,id=null,data){
    if(id){
        return service({
            method:"patch",
            url:url + id + "/",
            data:data,
            headers:{'Content-Type': 'application/json;charset=utf-8',}
        })
    }else{
        return service({
            method:"patch",
            url:url,
            data:data,
            headers:{'Content-Type': 'application/json;charset=utf-8',}
        })
    }
}
export function shanchu(url,id=null,data){
    if(id){
        return service({
            method:"delete",
            url:url + id+"/",
            params:data,
            headers:{'Content-Type': 'application/json;charset=utf-8',}
        })
    }else{
        return service({
            method:"delete",
            url:url,
            params:data,
            headers:{'Content-Type': 'application/json;charset=utf-8',}
        })
    }
}
export function get(url,id=null,data){
    if(id){
        console.log("有id的分支")
        return service({
            method:"get",
            url:url + id+"/",
            params:data,
            headers:{'Content-Type': 'application/json;charset=utf-8',}
        })
    }else {
        console.log("没有id的分支")
        return service({
            method:"get",
            url:url,
            params:data,
            headers:{'Content-Type': 'application/json;charset=utf-8',}
        })
    }
}

export function form(url,data){
    return service({
        method:"post",
        url:url,
        data:data,
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        // transformRequest: [function (data) {
        //     let ret = ''
        //     for (const it in data) {
        //         ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
        //     }
        //     return ret
        // }]
    })
}
